<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
</head>

<body>
    <div class="container">
        <h2 class="page-header">基本使用</h2>
        <button class="btn btn-primary">发送get请求</button>
        <button class="btn btn-primary">发送post请求</button>
        <button class="btn btn-primary">发送put请求</button>
        <button class="btn btn-primary">发送delete请求</button>
    </div>
    <script>
        // 获取按钮
        const btns = document.querySelectorAll('button');
        btns[0].onclick = function () {
            axios({
                method: 'get',
                url: "http://localhost:3000/posts"
            }).then(res => {
                console.log(res)
            })
        }
        // 添加一篇新得文章
        btns[1].onclick = function () {
            axios({
                method: 'post',
                url: "http://localhost:3000/posts",
                data: {
                    title: "今天天气不错",
                    author: "iverson"
                }
            }).then(res => {
                console.log(res)
            })
        }
        // 更改数据
        btns[2].onclick = function () {
            axios({
                method: 'put',
                url: "http://localhost:3000/posts/4",
                data: {
                    title: "今天天气不错",
                    author: "kobe"
                }
            }).then(res => {
                console.log(res)
            })
        }
        // 删除数据
        btns[3].onclick = function () {
            axios({
                method: 'delete',
                url: "http://localhost:3000/posts/5",
            }).then(res => {
                console.log(res)
            })
        }
    </script>
</body>

</html>